/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package ui.windows.components.panels.searchParametersPanel;

import com.toedter.calendar.JDateChooser;
import constants.Constants;
import processing.objects.SearchParameters;
import ui.windows.components.models.IntegerTextField;
import utils.properties.Configuration;

import javax.swing.*;
import java.util.Calendar;
import java.util.Date;

/**
 * Search parameters panel
 * Generates search parameters from user input
 *
 * @author rymek
 *         User: rymek
 *         Date: 19.07.11
 *         Time: 10:33
 */
public class SearchParamsPanel {

    private JPanel searchParametersPanel;

    private JEditorPane paramSearchString;

    private JSpinner paramStartTime;
    private JSpinner paramEndTime;

    private JDateChooser paramDateFrom;
    private JDateChooser paramDateTo;

    private IntegerTextField paramProcessingTime;
    private JCheckBox includeArchives;

    private Object parentFrame;

    /**
     * Constructor.
     * Initializes search parameters panel setting default values
     */
    public SearchParamsPanel() {
        this.setDefaultValues();
        this.paramSearchString.addKeyListener(new SearchParamsPanelKeyAdapter(this));
    }

    /**
     * Builds datetime start search parameter
     *
     * @return datetime start search parameter
     */
    String getDttmFrom() {
        return Configuration.dateFormat.format(this.paramDateFrom.getDate()) + " " + Configuration.timeFormat.format(((SpinnerDateModel) this.paramStartTime.getModel()).getDate());
    }

    /**
     * Builds datetime end search parameter
     *
     * @return datetime end search parameter
     */
    String getDttmTo() {
        return Configuration.dateFormat.format(this.paramDateTo.getDate()) + " " + Configuration.timeFormat.format(((SpinnerDateModel) this.paramEndTime.getModel()).getDate());
    }

    /**
     * Sets default values for SearchParamsPanel
     */
    private void setDefaultValues() {

        if (Configuration.production) {
            this.paramProcessingTime.setEditable(false);
            this.paramProcessingTime.setEnabled(false);
        }

        this.paramDateFrom.setDate(new Date());
        this.paramDateTo.setDate(new Date());
        this.paramProcessingTime.setText("0");
        this.paramSearchString.setText("");

        this.setDefaultValuesForDateSpinners();
    }

    /**
     * Sets default values for JSpinners to point
     * from 00:00:00 to 23:59:59
     */
    private void setDefaultValuesForDateSpinners() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, 0, 0, 0);
        this.paramStartTime.getModel().setValue(calendar.getTime());
        calendar.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, 23, 59, 59);
        this.paramEndTime.getModel().setValue(calendar.getTime());
    }

    /**
     * Generates SearchParameters object from
     * input fields
     *
     * @return SearchParameters object
     */
    public SearchParameters getSearchParameters() {

        SearchParameters searchParameters = new SearchParameters();
        searchParameters.setDttmFrom(this.getDttmFrom());
        searchParameters.setDttmTo(this.getDttmTo());
        searchParameters.setProcessingTime(this.paramProcessingTime.getText());
        searchParameters.setSearchString(this.paramSearchString.getText());

        if (this.includeArchives.isSelected()) searchParameters.setIncludeArchives(true);
        else searchParameters.setIncludeArchives(false);

        return searchParameters;
    }

    /**
     * Disables fields while Search is running
     */
    public void lockFieldsForSearch() {
        this.paramDateFrom.setEnabled(false);
        this.paramDateTo.setEnabled(false);
        this.paramStartTime.setEnabled(false);
        this.paramEndTime.setEnabled(false);
        this.paramSearchString.setEditable(false);
        this.paramSearchString.setEnabled(false);
        this.includeArchives.setEnabled(false);

        if (!Configuration.production) {
            this.paramProcessingTime.setEditable(false);
            this.paramProcessingTime.setEnabled(false);
        }
    }

    /**
     * Enables fields after Search is finished
     */
    public void unlockFields() {
        this.paramDateFrom.setEnabled(true);
        this.paramDateTo.setEnabled(true);
        this.paramStartTime.setEnabled(true);
        this.paramEndTime.setEnabled(true);
        this.paramSearchString.setEditable(true);
        this.paramSearchString.setEnabled(true);
        this.includeArchives.setEnabled(true);

        if (!Configuration.production) {
            this.paramProcessingTime.setEditable(true);
            this.paramProcessingTime.setEnabled(true);
        }
    }

    /**
     * Custom create for some of the components
     */
    private void createUIComponents() {

        this.paramStartTime = new JSpinner(new SpinnerDateModel());
        this.paramStartTime.setEditor(new JSpinner.DateEditor(this.paramStartTime, Constants.TIME_FORMAT));
        this.paramEndTime = new JSpinner(new SpinnerDateModel());
        this.paramEndTime.setEditor(new JSpinner.DateEditor(this.paramEndTime, Constants.TIME_FORMAT));

    }

    public JPanel getSearchParametersPanel() {
        return searchParametersPanel;
    }

    public Object getParentFrame() {
        return parentFrame;
    }

    public void setParentFrame(Object parentFrame) {
        this.parentFrame = parentFrame;
    }
}
